package com.inifitness.admin.core.config.captcha;

import java.awt.Font;

import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.captcha.generator.RandomGenerator;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 图形验证码配置
 *
 * @author sunjinfeng
 * @version 1.0.0
 * @since 2024/04/18 11:00:00
 */
@Log4j2
@RequiredArgsConstructor
@Configuration
public class CaptchaConfig {

  private final CaptchaProperties captchaProperties;

  /**
   * 验证码文字生成器
   *
   * @return CodeGenerator
   */
  @Bean
  public CodeGenerator codeGenerator() {
    String codeType = captchaProperties.getCode().getType();
    int codeLength = captchaProperties.getCode().getLength();
    if ("math".equalsIgnoreCase(codeType)) {
      return new MathGenerator(codeLength);
    } else if ("random".equalsIgnoreCase(codeType)) {
      return new RandomGenerator(codeLength);
    } else {
      throw new IllegalArgumentException("Invalid captcha generator type: " + codeType);
    }
  }

  /**
   * 验证码字体
   */
  @Bean
  public Font captchaFont() {
    String fontName = captchaProperties.getFont().getName();
    int fontSize = captchaProperties.getFont().getSize();
    int fontWeight = captchaProperties.getFont().getWeight();
    return new Font(fontName, fontWeight, fontSize);
  }
}
